from ncclient import manager
from lxml import etree
from xml.etree.cElementTree import parse
import pathlib

vsrx = '10.0.46.241'
# vsrx = '10.0.46.251'
user = 'admin'
passwd = 'networkTeam'
#connect()方法，连接工具，返回一个连接对象
#默认netconf服务器端是830端口,hostkey_verify是ssh验证问题
netconf_connection = manager.connect(host=vsrx,
                                     username=user,
                                     password=passwd,
                                     port=830,
                                     device_params={'name':'h3c'},
                                     hostkey_verify=False
                                     )
print(netconf_connection)
#查看在服务器端的连接会话id
id = netconf_connection.session_id
print(id)

#client_capabilities属性是客户端支持的netconf能力，返回的是一个迭代器对象
for each in netconf_connection.client_capabilities:
    print(each)

print(''.center(50,'#'))

#server_capabilities属性是服务端支持的netconf能力，返回的是一个迭代器对象
for each in netconf_connection.server_capabilities:
    print(each)

#get_config()，获取远程配置，返回的格式是基于xml格式
# vsrx_conf = netconf_connection.get_config(source='running')
# print(vsrx_conf)

'''测试get_config获取配置信息的时间

'''
import timeit

def test1():

    connct = manager.connect(host=vsrx, \
                             username=user, \
                             password=passwd, \
                             port=830, \
                             device_params={'name': 'h3c'}, \
                             hostkey_verify=False \
                             )

    connct.get_config(source='running')
start_time = timeit.default_timer()

test1()

end_time = timeit.default_timer()

print('execute time %s' % (end_time-start_time))

# config_e = etree.Element("config")
# configuration = etree.SubElement(config_e, "interface-configurations", nsmap = {None: 'http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg'})
# interface_cfg = etree.SubElement(configuration, "interface-configuration")
# active = etree.SubElement(interface_cfg, "active").text = 'act'
# interface_name = etree.SubElement(interface_cfg, "interface-name").text = 'GigabitEthernet0/0/0/0'
# description = etree.SubElement(interface_cfg, "description").text  = 'NETCONF configured - ' + current_time

f = pathlib.Path('10.0.47.250.xml')

config = f.read_text('utf-8')
'''
host='10.0.47.250'
user='admin'
passwd='networkTeam'
with manager.connect(host=host, port=830, username=user, password=passwd, device_params={'name':'h3c'}, hostkey_verify=False) as m:
    c = m.get_config(source='running').data_xml
    with open("%s.xml" % host, 'w') as f:
        f.write(c)
'''
print(netconf_connection.raise_mode)
netconf_connection.raise_mode = 0
edit_result = netconf_connection.edit_config(target='running', config=config, error_option='rollback-on-error')
print(edit_result)


# with open('10.0.47.250.xml', 'rt') as f:
#     tree = parse(f)
#
# for node in tree.iter():
#     print(node.tag)
#     print(node.attrib)

# config_rt = etree.Element('config')
# configacl = etree.SubElement(config_rt, attrib='',nsmap=)
# netconf_connection.get_config(source='running')


from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom

def prettify(elem):
    rough_string = tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent='    ')

config = Element('config', {'xmlns:xc': 'urn:ietf:params:xml:ns:netconf:base:1.0'})
top = SubElement(config, 'top', {'xmlns': 'http://www.h3c.com/netconf/config:1.0'})
# acl = SubElement(top, '')
print(prettify(config))